package com.example.archivemanagement.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.archivemanagement.entity.Archive;
import com.example.archivemanagement.service.ArchiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/archives")
@CrossOrigin(origins = "*", maxAge = 3600)
public class ArchiveController {

    @Autowired
    private ArchiveService archiveService;

    @PostMapping("/upload")
    @PreAuthorize("isAuthenticated()")
    public ResponseEntity<?> uploadArchive(
            @RequestParam("file") MultipartFile file,
            @RequestParam("name") String name,
            @RequestParam("category") String category,
            @RequestParam("securityLevel") String securityLevel,
            @RequestParam(value = "description", required = false) String description) {
        
        Archive archive = new Archive();
        archive.setName(name);
        archive.setCategory(category);
        archive.setSecurityLevel(securityLevel);
        archive.setDescription(description);
        
        Archive savedArchive = archiveService.uploadArchive(file, archive);
        return ResponseEntity.ok(savedArchive);
    }

    @GetMapping("/recent")
    public ResponseEntity<?> getRecentArchives(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        Page<Archive> archives = archiveService.getRecentArchives(page, size);
        return ResponseEntity.ok(archives);
    }

    @GetMapping("/search")
    public ResponseEntity<?> searchArchives(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String category,
            @RequestParam(required = false) String securityLevel,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        Page<Archive> archives = archiveService.searchArchives(keyword, category, securityLevel, page, size);
        return ResponseEntity.ok(archives);
    }

    @GetMapping("/{id}")
    public ResponseEntity<?> getArchive(@PathVariable Long id) {
        Archive archive = archiveService.getById(id);
        if (archive == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(archive);
    }

    @PutMapping("/{id}")
    @PreAuthorize("isAuthenticated()")
    public ResponseEntity<?> updateArchive(@PathVariable Long id, @RequestBody Archive archive) {
        archive.setId(id);
        Archive updatedArchive = archiveService.updateArchive(archive);
        return ResponseEntity.ok(updatedArchive);
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("isAuthenticated()")
    public ResponseEntity<?> deleteArchive(@PathVariable Long id) {
        archiveService.deleteArchive(id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/category/{category}")
    public ResponseEntity<?> getArchivesByCategory(@PathVariable String category) {
        List<Archive> archives = archiveService.getArchivesByCategory(category);
        return ResponseEntity.ok(archives);
    }

    @GetMapping("/security-level/{level}")
    public ResponseEntity<?> getArchivesBySecurityLevel(@PathVariable String level) {
        List<Archive> archives = archiveService.getArchivesBySecurityLevel(level);
        return ResponseEntity.ok(archives);
    }
} 